package com.lc.zpyh.ui.activity.order;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.zpyh.R;
import com.lc.zpyh.app.AppActivity;
import com.lc.zpyh.http.glide.GlideApp;
import com.lc.zpyh.http.request.AddPostACommentApi;
import com.lc.zpyh.http.request.SelectCommentContentApi;
import com.lc.zpyh.http.response.SelectCommentContentBean;
import com.lc.zpyh.other.IntentKey;
import com.lc.zpyh.util.CourseUtils;
import com.lc.zpyh.util.SPUtil;
import com.lc.zpyh.widget.UIHelper;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;

public class ShopCommentActivity extends AppActivity {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.title_bar_top)
    TitleBar titleBarTop;
    private String order_id="";
    private List<String> specificationIdList = new ArrayList<>();

    private List<String> commodityIdList = new ArrayList<>();

    private List<String> deliverySpeedList = new ArrayList<>();

    private List<String> healthSituationList = new ArrayList<>();
    private List<String> heatPreservationList = new ArrayList<>();
    private List<SelectCommentContentBean.ListBean.DetailsBean> dataList=new ArrayList();
    private BaseQuickAdapter<SelectCommentContentBean.ListBean.DetailsBean,  BaseViewHolder> pingjiaadapter;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_shopcomment;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        order_id = getIntent().getStringExtra("order_id");
        selectCommentContent();
        titleBarTop.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                finish();
            }

            @Override
            public void onTitleClick(View v) {

            }

            @Override
            public void onRightClick(View v) {
               //发表
                addPostAComment();

            }
        });
    }



    private void addPostAComment() {
        for (int i = 0; i < dataList.size(); i++) {
            commodityIdList.add(dataList.get(i).getCommodityId() + "");
            if (dataList.get(i).getSpecificationId()!=null) {
                specificationIdList.add(dataList.get(i).getSpecificationId() + "");
            }else {
                specificationIdList.add("0");
            }
            deliverySpeedList.add(dataList.get(i).getStar_num1());
            healthSituationList.add(dataList.get(i).getStar_num2());
            heatPreservationList.add(dataList.get(i).getStar_num3());
        }
        EasyHttp.post(this)
                .api(new AddPostACommentApi().setOrderid(order_id).setUserid(SPUtil.get(this, IntentKey.USERID,"")).setCommodityId(CourseUtils.listToString1(commodityIdList)).setSpecificationId(CourseUtils.listToString1(specificationIdList)).setDeliverySpeed(CourseUtils.listToString1(deliverySpeedList)).setHealthSituation(CourseUtils.listToString1(healthSituationList)).setHeatPreservation(CourseUtils.listToString1(heatPreservationList))).request(new HttpCallback<SelectCommentContentBean>(this) {

            @Override
            public void onSucceed(SelectCommentContentBean data) {
                UIHelper.startActivity(ShopCommentActivity.this,CommentSuccessfActivity.class);
                finish();
            }
            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }

    private void selectCommentContent() {

            EasyHttp.post(this)
                    .api(new SelectCommentContentApi().setOrderid(order_id)).request(new HttpCallback<SelectCommentContentBean>(this) {

                @Override
                public void onSucceed(SelectCommentContentBean data) {
                    if (data.getCode() == 0) {
                        if (data.getList().getDetails() != null && data.getList().getDetails().size() > 0) {
                            dataList.clear();
                            dataList.addAll(data.getList().getDetails());
                            for (SelectCommentContentBean.ListBean.DetailsBean bean :dataList) {
                                bean.setStar_num1("10");
                                bean.setStar_num2("10");
                                bean.setStar_num3("10");
                            }

                        pingjiaadapter = new BaseQuickAdapter<SelectCommentContentBean.ListBean.DetailsBean, BaseViewHolder>
                                (R.layout.item_comment_shop_goods, dataList) {

                            @Override
                            protected void convert(BaseViewHolder viewHolder, SelectCommentContentBean.ListBean.DetailsBean commentOrderBean) {

                                viewHolder.setText(R.id.tv_shop_name, commentOrderBean.getCommodityName());

                                GlideApp.with(getContext()).load(IntentKey.CAR_IMAGE+commentOrderBean.getMainImg()).into(((ImageView) viewHolder.getView(R.id.img_head)));
                                int starSpeed = Integer.parseInt(commentOrderBean.getStar_num1());
                                viewHolder.setImageResource(R.id.iv_speed_start_1, starSpeed >= 2 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);
                                viewHolder.setImageResource(R.id.iv_speed_start_2, starSpeed >= 4 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);
                                viewHolder.setImageResource(R.id.iv_speed_start_3, starSpeed >= 6 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);
                                viewHolder.setImageResource(R.id.iv_speed_start_4, starSpeed >= 8 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);
                                viewHolder.setImageResource(R.id.iv_speed_start_5, starSpeed >= 10 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);

                                int starhealth = Integer.parseInt(commentOrderBean.getStar_num2());
                                viewHolder.setImageResource(R.id.iv_hygiene_start_1, starhealth >= 2 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);
                                viewHolder.setImageResource(R.id.iv_hygiene_start_2, starhealth >= 4 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);
                                viewHolder.setImageResource(R.id.iv_hygiene_start_3, starhealth >= 6 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);
                                viewHolder.setImageResource(R.id.iv_hygiene_start_4, starhealth >= 8 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);
                                viewHolder.setImageResource(R.id.iv_hygiene_start_5, starhealth >= 10 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);
                                int starpreservation = Integer.parseInt(commentOrderBean.getStar_num3());
                                viewHolder.setImageResource(R.id.iv_effect_start_1, starpreservation >= 2 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);
                                viewHolder.setImageResource(R.id.iv_effect_start_2, starpreservation >= 4 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);
                                viewHolder.setImageResource(R.id.iv_effect_start_3, starpreservation >= 6 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);
                                viewHolder.setImageResource(R.id.iv_effect_start_4, starpreservation >= 8 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);
                                viewHolder.setImageResource(R.id.iv_effect_start_5, starpreservation >= 10 ? R.mipmap.ic_comment_full : R.mipmap.ic_comment_empty);

                                viewHolder.getView(R.id.iv_speed_start_1).setOnClickListener(view -> {
                                    commentOrderBean.setStar_num1("2");
                                    pingjiaadapter.notifyDataSetChanged();
//
                                });
                                viewHolder.getView(R.id.iv_speed_start_2).setOnClickListener(view -> {
                                    commentOrderBean.setStar_num1("4");
                                    pingjiaadapter.notifyDataSetChanged();
                                });
                                viewHolder.getView(R.id.iv_speed_start_3).setOnClickListener(view -> {
                                    commentOrderBean.setStar_num1("6");
                                    pingjiaadapter.notifyDataSetChanged();
                                });
                                viewHolder.getView(R.id.iv_speed_start_4).setOnClickListener(view -> {
                                    commentOrderBean.setStar_num1("8");
                                    pingjiaadapter.notifyDataSetChanged();
                                });
                                viewHolder.getView(R.id.iv_speed_start_5).setOnClickListener(view -> {
                                    commentOrderBean.setStar_num1("10");
                                    pingjiaadapter.notifyDataSetChanged();
                                });
                                    viewHolder.getView(R.id.iv_hygiene_start_1).setOnClickListener(view -> {
                                        commentOrderBean.setStar_num2("2");
                                        pingjiaadapter.notifyDataSetChanged();
                                    });
                                    viewHolder.getView(R.id.iv_hygiene_start_2).setOnClickListener(view -> {
                                        commentOrderBean.setStar_num2("4");
                                        pingjiaadapter.notifyDataSetChanged();
                                    });
                                    viewHolder.getView(R.id.iv_hygiene_start_3).setOnClickListener(view -> {
                                        commentOrderBean.setStar_num2("6");
                                        pingjiaadapter.notifyDataSetChanged();
                                    });
                                    viewHolder.getView(R.id.iv_hygiene_start_4).setOnClickListener(view -> {
                                        commentOrderBean.setStar_num2("8");
                                        pingjiaadapter.notifyDataSetChanged();
                                    });
                                    viewHolder.getView(R.id.iv_hygiene_start_5).setOnClickListener(view -> {
                                        commentOrderBean.setStar_num2("10");
                                        pingjiaadapter.notifyDataSetChanged();

                                });
                                viewHolder.getView(R.id.iv_effect_start_1).setOnClickListener(view -> {
                                    commentOrderBean.setStar_num3("2");
                                    pingjiaadapter.notifyDataSetChanged();
                                });
                                viewHolder.getView(R.id.iv_effect_start_2).setOnClickListener(view -> {
                                    commentOrderBean.setStar_num3("4");
                                    pingjiaadapter.notifyDataSetChanged();
                                });
                                viewHolder.getView(R.id.iv_effect_start_3).setOnClickListener(view -> {
                                    commentOrderBean.setStar_num3("6");
                                    pingjiaadapter.notifyDataSetChanged();
                                });
                                viewHolder.getView(R.id.iv_effect_start_4).setOnClickListener(view -> {
                                    commentOrderBean.setStar_num3("8");
                                    pingjiaadapter.notifyDataSetChanged();
                                });
                                viewHolder.getView(R.id.iv_effect_start_5).setOnClickListener(view -> {
                                    commentOrderBean.setStar_num3("10");
                                    pingjiaadapter.notifyDataSetChanged();

                                });
                            }
                        };


                        recyclerView.setAdapter(pingjiaadapter);
                    }
                        }

                    }


                @Override
                public void onFail(Exception e) {
                    super.onFail(e);

                }
            });
        }

}
